package org.footballmanager.pandafootball.system.controller;

import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.domain.FootLevelUp;
import org.footballmanager.pandafootball.system.form.impl.FootLevelUpSearchForm;
import org.footballmanager.pandafootball.system.service.FootDanGradingService;
import org.footballmanager.pandafootball.system.service.FootLevelUpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
@SessionAttributes("footLevelUpSearchForm")
@RequestMapping("/foot/footLevelUp")
public class FootLevelUpController extends AddAndEditController<FootLevelUp> {
    /* 实体名称 */
    private static final String ENTITYNAME = "footLevelUp";
    private static final String ADDPAGE = String.format(Constants.ADDPAGE, Constants.FOOT, ENTITYNAME);
    private static final String EDITPAGE = String.format(Constants.EDITPAGE, Constants.FOOT, ENTITYNAME);
    private static final String LISTPAGE = String.format(Constants.LISTPAGE, Constants.FOOT, ENTITYNAME);
    private static final String VIEWPAGE = String.format(Constants.VIEWPAGE, Constants.FOOT, ENTITYNAME);
    private static final String REDIRECT = String.format(Constants.REDIRECT, Constants.FOOT, ENTITYNAME);

    @Autowired
    private FootLevelUpService footLevelUpService;
    @Autowired
    private FootDanGradingService footDanGradingService;

    /**
     * 抽象方法
     *
     * @return 实体名称
     */
    @Override
    protected String getEntityName() {
        return ENTITYNAME;
    }

    /**
     * 初始化
     *
     * @param map                   参数
     * @param footLevelUpSearchForm 条件参数
     */
    @ModelAttribute
    public void init(Model map, @ModelAttribute FootLevelUpSearchForm footLevelUpSearchForm) {
        map.addAttribute("danGradingList", footDanGradingService.getall());
        FootLevelUpSearchForm newFootLevelUpSearchForm = new FootLevelUpSearchForm();
        if (footLevelUpSearchForm != null) {
            newFootLevelUpSearchForm = footLevelUpSearchForm;
        }
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), newFootLevelUpSearchForm);
    }

    /**
     * 积分升级对应列表
     *
     * @param map                   参数
     * @param footLevelUpSearchForm 条件
     * @return 页面
     */
    @RequestMapping(value = "/search")
    public String search(Model map, @ModelAttribute FootLevelUpSearchForm footLevelUpSearchForm) {
        footLevelUpService.search(ENTITYNAME, map, footLevelUpSearchForm);
        return LISTPAGE;
    }

    /**
     * 删除积分升级对应记录
     *
     * @param id      主键
     * @param request 请求
     * @return 页面
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(@RequestParam Long id, HttpServletRequest request) {
        footLevelUpService.delete(id, request);
        return REDIRECT;
    }

    /**
     * 删除积分升级对应列表
     *
     * @param request               请求
     * @param footLevelUpSearchForm 条件
     * @return 页面
     */
    @RequestMapping(value = "/deleteall", method = RequestMethod.POST)
    public String deleteall(HttpServletRequest request, @ModelAttribute FootLevelUpSearchForm footLevelUpSearchForm) {
        footLevelUpService.deleteAll(footLevelUpSearchForm, request);
        return REDIRECT;
    }

    /**
     * 打开添加积分升级对应页面
     *
     * @param map 参数
     * @return 页面
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String addAddPage(Model map) {
        map.addAttribute(ENTITYNAME, new FootLevelUp());
        return ADDPAGE;
    }

    /**
     * 打开编辑积分升级对应页面
     *
     * @param map 参数
     * @param id  主键
     * @return 页面
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String openEditPage(Model map, @RequestParam Long id) {
        map.addAttribute(ENTITYNAME, footLevelUpService.get(id));
        return EDITPAGE;
    }

    /**
     * 浏览积分升级对应页面
     *
     * @param map                   参数
     * @param id                    主键
     * @param footLevelUpSearchForm 条件
     * @return 页面
     */
    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String openViewPage(Model map, @RequestParam Long id, @ModelAttribute FootLevelUpSearchForm footLevelUpSearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), footLevelUpSearchForm);
        map.addAttribute(ENTITYNAME, footLevelUpService.get(id));
        return VIEWPAGE;
    }

    @RequestMapping(value = "/calculate", method = RequestMethod.GET)
    @ResponseBody
    public String calculate(HttpServletRequest request) {
        footLevelUpService.calculateLevel(request);
        return "ok";
    }
}
